using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Acms.WebControls
{
    public class DropDownField : BaseDataField
    {
        private string dataSourceId;
        public string DataSourceId
        {
            get { return dataSourceId; }
            set { dataSourceId = value; }
        }

        private string dataTextField;
        public string DataTextField
        {
            get { return dataTextField; }
            set { dataTextField = value; }
        }

        private string dataValueField;
        public string DataValueField
        {
            get { return dataValueField; }
            set { dataValueField = value; }
        }

        private string displayField;
        public string DisplayField
        {
            get { return displayField; }
            set { displayField = value; }
        }
	

        public string MyProperty
        {
            get { return DisplayField; }
            set { DisplayField = value; }
        }
	

        private bool hasEmptyValue;
        public bool HasEmptyValue
        {
            get { return hasEmptyValue; }
            set { hasEmptyValue = value; }
        }

        private string emptyValueText;
        public string EmptyValueText
        {
            get { return emptyValueText; }
            set { emptyValueText = value; }
        }

        protected override System.Web.UI.WebControls.DataControlField CreateField()
        {
            return new DropDownField();
        }

        protected override void DataBindDisplayControls(DataControlFieldCell cell, object dataItem, int dataItemIndex, int displayIndex)
        {
            Label lbl = cell.Controls[0] as Label;
            if (lbl != null && !string.IsNullOrEmpty(DisplayField))
            {
                object val = DataBinder.Eval(dataItem, DisplayField);
                if (val != null)
                {
                    lbl.Text = val.ToString();
                }
            }
            else
            {
                base.DataBindDisplayControls(cell, dataItem, dataItemIndex, displayIndex);
            }
        }

        protected override void InitEditControls(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex)
        {
            DropDownList ctrl = new DropDownList();
            ctrl.ID = GetUniqueId(rowIndex, "ddl");
            ctrl.DataSourceID = this.DataSourceId;
            ctrl.DataTextField = this.DataTextField;
            ctrl.DataValueField = this.DataValueField;
            ctrl.AppendDataBoundItems = true;
            if (HasEmptyValue)
            {
                ctrl.Items.Add(new ListItem(EmptyValueText, ""));
            }

            cell.Controls.Add(ctrl);
        }

        protected override void DataBindEditControls(DataControlFieldCell cell, object dataItem, int dataItemIndex, int displayIndex)
        {
            DropDownList ctrl = cell.Controls[0] as DropDownList;
            if (ctrl != null)
            {
                object val = DataBinder.Eval(dataItem, DataField);
                if(val != null)
                    ctrl.SelectedValue = val.ToString();
            }
        }


        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            if (cell.Controls.Count > 0 && !string.IsNullOrEmpty(this.DataField))
            {
                DropDownList ctrl = cell.Controls[0] as DropDownList;
                if (ctrl != null)
                {
                    dictionary.Add(DataField, ctrl.SelectedValue);
                }
            }

        }

    }
}
